package com.ellecity06.cwhiskymall.ui.register

import androidx.lifecycle.viewModelScope
import com.ellecity06.common.base.BaseMviViewModel
import com.ellecity06.common.ktx.md5Encrypt
import com.ellecity06.common.ktx.postEvent
import com.ellecity06.common.utils.ToastUtils
import com.ellecity06.cwhiskymall.bean.request.RegisterRequest
import com.ellecity06.cwhiskymall.bean.request.SendSmsCodeRequest
import com.ellecity06.cwhiskymall.http.ServerConfig
import com.ellecity06.cwhiskymall.http.request
import kotlinx.coroutines.launch

/**
 * @author ellecity06
 * @time 2023/4/13 16:10
 * @des
 */
class RegisterViewModel : BaseMviViewModel<RegisterState, RegisterIntent>() {
    private var checked: Boolean = false
    override suspend fun collectIntent(it: RegisterIntent) {
        when (it) {
            is RegisterIntent.ChangeCheck -> {
                checked = !checked
                mState.postEvent(RegisterState.Checked(checked))
            }
            // 获取图片验证码
            is RegisterIntent.SendSmsCode -> fetchSmsCode(it.imagCode, it.phone)
            is RegisterIntent.Register -> register(it)
            is RegisterIntent.GetPicCode -> getPicCode(it.phone)
            else -> {}
        }
    }

    private fun register(it: RegisterIntent.Register) {
        if (it.phone.isEmpty() || it.phone.length < 11) {
            ToastUtils.show("请输入正确的手机号")
            return
        }
        if (it.code.isEmpty()) {
            ToastUtils.show("请输入验证码")
            return
        }
        if (it.password.isEmpty()) {
            ToastUtils.show("请输入密码")
            return
        }
        if (it.rePassword.isEmpty()) {
            ToastUtils.show("请再次输入密码")
            return
        }
        if (it.password != it.rePassword) {
            ToastUtils.show("两次密码不一致")
            return
        }
        if (!checked) {
            ToastUtils.show("请阅读并同意用户协议和隐私政策")
            return
        }
        request(
            api = { register(RegisterRequest(it.code, it.password?.md5Encrypt(), it.phone)) },
            success = {
                mState.postEvent(RegisterState.RegisterSuccess)
            })
    }

    private fun fetchSmsCode(imageCode: String, phone: String) {
        if (phone.isEmpty() || phone.length < 11) {
            ToastUtils.show("请输入正确的手机号")
            return
        }
        if (imageCode.isEmpty()) {
            ToastUtils.show("请输入图形验证码")
            return
        }
        request(api = { smsCode(SendSmsCodeRequest(1, imageCode, phone)) }, success = {
            mState.postEvent(RegisterState.SmdCodeSuccess)
        }, error = {
            showToast(it.message)
            getPicCode(phone)
        })
    }

    /**
     * 获取图片验证码
     */
    fun getVerifyCode() {


    }

    /**
     * 登录
     */
    fun login(userName: String, pwd: String, code: String) {
    }

    private fun getPicCode(phone: String) {
        if (phone.isEmpty() || phone.length < 11) {
            ToastUtils.show("请输入正确的手机号")
            return
        }
        val url =
            "${ServerConfig.baseHost}api/web/auth/sms/sendpicVerifyCode?phone=${phone}&timestamp=${System.currentTimeMillis()}"
        viewModelScope.launch {
            mState.postEvent(RegisterState.GetPicCodeSuccess(url))
        }

    }
}